import { createRouter, createWebHistory } from 'vue-router'
// import HomeView from '../views/HomeView.vue'
import FileOperation from '@/components/main/FileOperation.vue'
import Logs from '@/components/main/Logs.vue'
import Notice from '@/components/main/Notice.vue'
import RecycleBin from '@/components/main/RecycleBin.vue'
import User from '@/components/main/User.vue'
import Welcome from '@/components/main/Welcome.vue'
import NotFound from '@/views/404.vue'
import ForgetPassword from '@/views/ForgetPassword.vue'
import Home from '@/views/Home.vue'
import Login from '@/views/Login.vue'
import Person from '@/views/Person.vue'
import Register from '@/views/Register.vue'
import changePassword from '@/views/changePassword.vue'
import Orders from '../components/main/Orders.vue'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
   
    {
      path: '/',
      name: 'login',
      component: Login
    },
    {
      path: '/register',
      name:'register',
      component: Register
    },
    {
      path: '/home',
      name: 'home',
      component: Home,
      redirect: '/welcome',
      children: [
        {
          path: '/welcome',
          name: 'welcome',
          component: Welcome,
          meta: {
            title: '首页'
          }
        },
        {
          path: '/file',
          name: 'file',
          component: FileOperation,
          meta: {
            title: '文件管理'
          }
        },
        {
          path: '/recycleBin',
          name:'recycleBin',
          component: RecycleBin,
          meta: {
            title: '回收站'
          }
        },
        {
          path: '/user',
          name: 'user',
          component: User,
          meta: {
            title: '用户管理'
          }
        },
        {
          path: '/notice',
          name: 'notice',
          component: Notice,
          meta: {
            title: '通知公告'
          }
        },
        {
          path: '/logs',
          name: 'logs',
          component: Logs,
          meta: {
            title: '日志管理'
          }
        },
        {
          path: '/orders',
          name: 'orders',
          component: Orders,
          meta: {
            title: '订单管理'
          }
        }
      ]
    },
    {
      path: '/person',
      name: 'person',
      component: Person,
    },
    {
          path: '/change',
          name: 'change',
          component: changePassword,
    },
    {
        path: '/forget',
        name: 'forget',
        component: ForgetPassword,
    },
    {
      path: '/404',
      name: '404',
      component: NotFound
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'notFound',
      component: NotFound
    }
  ]
})

//路由守卫
router.beforeEach((to,from,next)=>{
  const user = JSON.parse(localStorage.getItem("user"));
  const token = user? user.token : null
//   console.log(token)
//   //检查用户的登录状态
//   // const token = localStorage.getItem('token')
  if(token!=null){
    if (to.path === '/') {
      next('/home') // 系统根路由
    }
  }
//   if(token==null){
//     ElMessage({
//       showClose: true,
//       message: "请先登录",
//       type: "error",
//     })
//   router.push({
//       path: '/'
//   })
//   }
  
  const nextRoute = [
    "/welcome",
    "/file",
    "/recycleBin",
    "/user",
    "/notice",
    "/logs",
    "/orders"
  ];
  if (nextRoute.indexOf(to.path) >= 0) {
    if (token==null) {
         ElMessage({
            showClose: true,
            message: "请先登录",
            type: "error",
          })
        router.push({
            path: '/'
        })
    }
  }
  
//   if (to.path === '/login') {
//     if (token!=null) {
//       next('/home') // 系统根路由
//   }
//   }
  next(); //必须要有
  })
  //防止回退操作
  router.afterEach((to, from) => {
    history.pushState(history.state, null, location.protocol + '//' + location.host  + to.path)
   })
export default router
